package graphics;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JComboBox;

import ship.Ship;
import util.Point;

import main.Game;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class GameWindow extends JFrame {
	public static final int WINDOW_X = 1400;
	public static final int WINDOW_Y = 750;
	
	private JPanel contentPane;
	private JPanel center;
	private JPanel controlPanel;
	
	private JComboBox rightTube ;
	private JComboBox centerTube;;
	private JComboBox leftTube ;
	
	private JProgressBar leftProgressBar = new JProgressBar();
	private JProgressBar rightProgressBar = new JProgressBar();

	private JProgressBar shieldBar;
	
	private MiniLabel miniMap = new MiniLabel();
	
	private JLabel static1 = new JLabel();
	private JLabel static2 = new JLabel();
	private JLabel static3 = new JLabel();
	
	private JLabel[] stat = { static1, static2, static3 };
	
	private ShipStatus shipStatus;

	private Ship ship;

	/**
	 * Create the frame.
	 */
	public GameWindow() {
		ship = Game.game.getShip();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(0,0,WINDOW_X, WINDOW_Y);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		controlPanel = new JPanel();
		controlPanel.setBounds(1096, 0, 298, 722);
		contentPane.add(controlPanel);
		controlPanel.setLayout(null);
		

		leftTube = new JComboBox(ship.getWeapons());
		leftTube.setBounds(94, 359, 126, 27);
		leftTube.setFocusable(false);
		controlPanel.add(leftTube);
		
		JLabel label = new JLabel("Left Tube:");
		label.setBounds(18, 363, 92, 16);
		controlPanel.add(label);
		
		
		leftProgressBar.setBounds(18, 391, 136, 27);
		controlPanel.add(leftProgressBar);
		
		JLabel label_1 = new JLabel("Right Tube:");
		label_1.setBounds(18, 296, 92, 16);
		controlPanel.add(label_1);
		
		rightTube = new JComboBox(ship.getWeapons());
		rightTube.setBounds(94, 292, 126, 27);
		rightTube.setFocusable(false);
		controlPanel.add(rightTube);
		
		
		rightProgressBar.setBounds(18, 324, 136, 27);
		controlPanel.add(rightProgressBar);
		
		centerTube = new JComboBox();
		centerTube.setBounds(94, 426, 126, 27);
		centerTube.setFocusable(false);
		controlPanel.add(centerTube);
		
		JLabel label_2 = new JLabel("Special:");
		label_2.setBounds(18, 430, 61, 16);
		controlPanel.add(label_2);
		
		JProgressBar specialProgressBar = new JProgressBar();
		specialProgressBar.setBounds(18, 464, 136, 27);
		controlPanel.add(specialProgressBar);
		
		shieldBar = new JProgressBar();
		shieldBar.setBounds(10, 50, 210, 20);
		controlPanel.add(shieldBar);
		

		miniMap.setBounds(28, 535, 170, 133);
		controlPanel.add(miniMap);
		
		
		static1.setIcon(new ImageIcon("images/static1.png"));
		static1.setBounds(0, 670, 327, 52);
		controlPanel.add(static1);
		
		
		static2.setIcon(new ImageIcon("images/static1.png"));
		static2.setBounds(230, 43, 81, 616);
		controlPanel.add(static2);
		
		
		static3.setIcon(new ImageIcon("images/static1.png"));
		static3.setBounds(0, 0, 327, 41);
		controlPanel.add(static3);
		
		JButton fireRight = new JButton("Fire");
		fireRight.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ship.changeRightTube(rightTube.getSelectedIndex());
				ship.fireWeapon(true);
			}
		});
		fireRight.setBounds(156, 324, 65, 27);
		fireRight.setFocusable(false);
		controlPanel.add(fireRight);
		
		JButton fireLeft = new JButton("Fire");
		fireLeft.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ship.changeLeftTube(leftTube.getSelectedIndex());
				ship.fireWeapon(false);
			}
		});
		fireLeft.setBounds(156, 391, 65, 27);
		fireLeft.setFocusable(false);
		controlPanel.add(fireLeft);
		
		JButton fireCenter = new JButton("Fire");
		fireCenter.setBounds(156, 464, 65, 27);
		fireCenter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ship.changeCenterTube(centerTube.getSelectedIndex());
				ship.fireSpecial();
			}
		});
		fireCenter.setFocusable(false);
		controlPanel.add(fireCenter);
		
		JProgressBar progressBar = new JProgressBar();
		progressBar.setBounds(18, 503, 194, 20);
		controlPanel.add(progressBar);
		/*
		JLabel lblNewLabel = new JLabel("New label");
		lblNewLabel.setBounds(10, 75, 215, 201);
		controlPanel.add(lblNewLabel);
		*/
		center = new JPanel();
		center.setBounds(0, 0, 1394, 722);
		contentPane.add(center);
		
		center.addMouseListener(new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent arg0) {
				Point target = new Point(arg0.getX(), arg0.getY());
				Game.game.setTarget(target);
				System.out.println(target);
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
	}
	
	public void begin() {		
		shipStatus = new ShipStatus(ship);
		shipStatus.setBounds(10, 45, 215, 241);
		controlPanel.add(shipStatus);
		
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(WINDOW_X, WINDOW_Y);

		show();
	}
	
	public void update() {
		shipStatus.repaint();
		shieldBar.setValue((int) (ship.getShields() * 100));
		miniMap.repaint();
	}
	
	
	
	public JLabel[] getStatic() {
		return stat;
	}
	
	public Graphics getGraphicsForPainting() {
		return center.getGraphics();
	}
}
